package cn.itcast.mq.listener;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class SpringRabbitListener {

    /**
     * 简单模型-消费者
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void simpleListener(String msg){
        System.out.println("简单模型-消费者-消费到消息：" + msg);
    }

    /**
     * 工作队列模型-消费者
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void workListener1(String msg) throws InterruptedException {
        System.out.println("工作队列模型-消费者-消费到消息1：" + msg);
        TimeUnit.MILLISECONDS.sleep(20);
    }

    /**
     * Fanout广播队列模型-消费者
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutListener1(String msg) throws InterruptedException {
        System.out.println("Fanout广播模型-消费者-消费到消息1：" + msg);
        TimeUnit.MILLISECONDS.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void fanoutListener2(String msg) throws InterruptedException {
        System.out.println("Fanout广播模型-消费者-消费到消息2：" + msg);
        TimeUnit.MILLISECONDS.sleep(200);
    }

    /**
     * Direct 路由队列模型-消费者
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "direct.exchange"),
            key = {"yellow","red"}
    ))
    public void directListener1(String msg) throws InterruptedException {
        System.out.println("Fanout广播模型-消费者-消费到消息1：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "direct.exchange"),
            key = {"blue","red"}
    ))
    public void directListener2(String msg) throws InterruptedException {
        System.out.println("Fanout广播模型-消费者-消费到消息2：" + msg);
    }
}